package com.frame.platform.user;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import com.frame.database.PageResult;
import com.frame.pageform.*;
import com.frame.pageform.annotation.Login;
import com.frame.pageform.component.JsonObject;
import com.frame.platform.post.PostDao;
import com.frame.session.UserSession;
import com.frame.util.Pagination;

/**
 * 用户管理
 * @author whf
 *
 */
@Login
public class UserManagePage extends DataPageForm{

	private Wfuser bean;
	private String actions;
	private String userName;
	private String loginName;
	private String deptId;
	private String state;
	private String selectDepts;
	private String oldPwd;
	private String newPwd;
	
	/**用户赋权属性**/
	private List deptList;
	private List rankList;
	private List selRankList;
	private String rankName;
	private String[] userRankIds;
	private List postList;
	private List userPosts;
	private String[] postIds;
	
	static{
		initForward("/platform/user/user.page");
	}
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if("addShow".equals(getActions())){
			setActions("add");
//			setPostList(new PostDao().getList());
//			setUserPosts(new ArrayList());
			return forward("add");
		}
		else if("add".equals(getActions())){
			UserDao dao=new UserDao();
			JsonObject jsonObj=new JsonObject();
			if(dao.checkLoginName(getBean().getLoginname(), getBean().getUserid())){
				dao.addUser(getBean(), getSelectDepts().split(","));
//				new PostDao().addUserPost(getBean().getUserid(), getPostIds());
				jsonObj.getJsonPage().showOkMsg("添加成功!");
				jsonObj.getJsonPage().setListFlesh(true);
				jsonObj.getJsonPage().setResetForm(true);
				jsonObj.put("state", "addok");
			}else
			    jsonObj.getJsonPage().showErrorMsg("已经存在相同账户了！");
			return jsonObj.toString();
		}
		else if("updateShow".equals(getActions())){
			bean=new UserDao().getUser(getPrimaryKey_()[0]);
//			setPostList(new PostDao().getList());
//			setUserPosts(new PostDao().getUserPostList(getPrimaryKey_()[0]));
			setActions("update");
			setNew_(false);
			return forward("add");
		}
		else if("update".equals(getActions())){
			setNew_(false);
			UserDao dao=new UserDao();
			JsonObject jsonObj=new JsonObject();
			if(dao.checkLoginName(getBean().getLoginname(), getBean().getUserid())){
				dao.updateUser(getBean(), getSelectDepts().split(","));
//				new PostDao().updateUserPost(getBean().getUserid(), getPostIds());
				jsonObj.getJsonPage().showOkMsg("修改成功!");
				jsonObj.getJsonPage().setListFlesh(true);
				jsonObj.getJsonPage().closeWin();
				jsonObj.put("state", "updateok");
			}else{
			    jsonObj.getJsonPage().showAlertMsg("已经存在相同账户了！");
			}
			return jsonObj.toString();
		}
		else if("del".equals(getActions())){
			new UserDao().delUser(getPrimaryKey_(),getDeptId());
			initData();
		}
		else if("updateState0".equals(getActions())){
			//冻结用户
			new UserDao().updateUserState(getPrimaryKey_()[0],new Integer(0));
			initData();
		}
		else if("updateState1".equals(getActions())){
			//激活用户
			new UserDao().updateUserState(getPrimaryKey_()[0],new Integer(1));
			initData();
		}
		else if("setRankShow".equals(getActions())){
			UserDao dao=new UserDao();
			bean=dao.getUser(getBean().getUserid());
			deptList=dao.getUserDeptList(getBean().getUserid());
			rankList=dao.getRankList(getRankName());
			selRankList=dao.getUserRankList(getBean().getUserid(), getDeptId());
			setPostList(new PostDao().getList());
            setUserPosts(new PostDao().getUserPostList(getBean().getUserid(), getDeptId()));
//			selectDepts="";deptId="";
//			for(int i=0;i<deptList.size();i++){
//			    Wfdept dept=(Wfdept)deptList.get(i);
//			    if(i!=0){selectDepts+="；";deptId+=",";};
//			    selectDepts+=dept.getName();
//			    deptId+=dept.getId();
//			}
			setActions("setRank");
			return forward("userrank");
		}
		else if("setRank".equals(getActions())){
			//分配权限
			UserDao dao=new UserDao();
			dao.addUserRank(getBean().getUserid(), getDeptId().split(","), getUserRankIds());
//			rankList=dao.getRankList(getRankName());
//			deptList=dao.getUserDeptList(getBean().getUserid());
//			rankList=dao.getRankList(getRankName());
//			selRankList=dao.getUserRankList(getBean().getUserid(), getDeptId());
			new PostDao().updateUserPost(getBean().getUserid(),getDeptId(), getPostIds());
			JsonObject jsonObj=new JsonObject();
			jsonObj.getJsonPage().showOkMsg("设置成功!");
			jsonObj.getJsonPage().closeWin();
			return jsonObj.toString();
		}
		else if("searchRank".equals(getActions())){
			//查找权限
			UserDao dao=new UserDao();
			rankList=dao.getRankList(getRankName());
			JsonObject json=new JsonObject();
			json.put("data", rankList);
			return json.toString();
		}
		else if("updatePwdShow".equals(getActions())){
			bean=new UserDao().getUser(getPrimaryKey_()[0]);
			setActions("updatePwd");
			return forward("pwd");
		}
		else if("updatePwd".equals(getActions())){
			JsonObject jsonObj=new JsonObject();
			if(getNewPwd()==null || "".equals(getNewPwd()))
			    jsonObj.getJsonPage().showInfoMsg("密码不能为空!");
			else{
				new UserDao().updataPwd(getBean().getUserid(), getNewPwd());
				jsonObj.getJsonPage().showOkMsg("密码修改成功!");
				jsonObj.getJsonPage().closeWin();
			}
			
			return jsonObj.toString();
		}else if("updateMyPwd".equals(getActions())){
			String uid=UserSession.getAdminUser().getUserId();
			UserDao dao=new UserDao();
			JsonObject jsonObj=new JsonObject();
			if(dao.checkPwd(uid, getOldPwd())){
				dao.updataPwd(uid, getNewPwd());
				jsonObj.put("state", "ok");
			}else
				jsonObj.put("state", "olderr");
			return jsonObj.toString();
		}
		else{
			initData();
		}
		return forward();
	}
	
	private void initData() throws Exception{
		if(getDeptId()==null || "".equals(getDeptId()))
			setDeptId("1");
		PageResult ps=new UserDao().getUserList(getUserName(),getLoginName(),getDeptId(),getPageSize_(),getPageNum());
		setResultList_(ps.getList());
		setActions("");
		Pagination pageing=new Pagination(ps.getPageSize(),ps.getPageNo(),ps.getPageCount(),getResultList_().size(),ps.getResultCount());
		setPagination(pageing);
	}
	public String getDeptUsers() throws Exception{
	    JsonObject json=new JsonObject();
	    List list=new UserDao().getDeptUserList(getDeptId());
	    json.put("data", list);
	    return json.toString();
	}
	public String changeMyPwdShow(){
	    return "/platform/user/changeMyPwd.jsp";
	}
	public String changeMyPwd_save() throws HibernateException, Exception{
	    String uid=UserSession.getAdminUser().getUserId();
        UserDao dao=new UserDao();
        if(dao.checkPwd(uid, getOldPwd())){
            dao.updataPwd(uid, getNewPwd());
            getJsonObject().getJsonPage().showAlertMsg("密码修改成功!");
            getJsonObject().put("state", "ok");
        }else
            getJsonObject().getJsonPage().showAlertMsg("旧密码输入错误!");
        return getJsonObject().toString();
    }

	public String getActions() {
		return actions;
	}

	public void setActions(String actions) {
		this.actions = actions;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Wfuser getBean() {
		return bean;
	}

	public void setBean(Wfuser bean) {
		this.bean = bean;
	}

	public String getSelectDepts() {
		return selectDepts;
	}

	public void setSelectDepts(String selectDepts) {
		this.selectDepts = selectDepts;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public List getRankList() {
		return rankList;
	}

	public void setRankList(List rankList) {
		this.rankList = rankList;
	}

	public List getSelRankList() {
		return selRankList;
	}

	public void setSelRankList(List selRankList) {
		this.selRankList = selRankList;
	}

	public String getRankName() {
		return rankName;
	}

	public void setRankName(String rankName) {
		this.rankName = rankName;
	}

	public String[] getUserRankIds() {
		return userRankIds;
	}

	public void setUserRankIds(String[] userRankIds) {
		this.userRankIds = userRankIds;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

    @Override
    public boolean toUpdate() throws Exception {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public String show() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String toDel() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    public List getPostList() {
        return postList;
    }

    public void setPostList(List postList) {
        this.postList = postList;
    }

    public String[] getPostIds() {
        return postIds;
    }

    public void setPostIds(String[] postIds) {
        this.postIds = postIds;
    }

    public List getUserPosts() {
        return userPosts;
    }

    public void setUserPosts(List userPosts) {
        this.userPosts = userPosts;
    }

}
